package com.zyp.test.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;

/**
 * Created by zyp on 2017/9/11.
 */

public class CircleImageView extends AppCompatImageView {
    private Context mContext;
    public CircleImageView(Context context) {
        super(context);
        mContext = context;
    }

    public CircleImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    public CircleImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Drawable drawable = getDrawable();
        if (drawable == null){
            return;
        }
        if(getWidth() == 0 || getHeight() == 0){
            return;
        }
        if(!(drawable instanceof BitmapDrawable)){
            return;
        }
       Bitmap b = ((BitmapDrawable) drawable).getBitmap();
        if(b == null){
            return;
        }
        Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888,true);
        int width = getWidth();
        Bitmap roundBitmap = drawCroppedBitmap(bitmap, width);
        canvas.drawBitmap(roundBitmap,0,0,null);
    }
    private Bitmap drawCroppedBitmap(Bitmap bitmap,int r){
        Bitmap sBitmap;
        sBitmap = Bitmap.createScaledBitmap(bitmap,r - DensityUtil.px2dip(mContext,5),r - DensityUtil.px2dip(mContext,5),true);
        Bitmap outBitmap = Bitmap.createBitmap(sBitmap.getWidth() + DensityUtil.px2dip(mContext,5), sBitmap.getHeight() + DensityUtil.px2dip(mContext,5), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(outBitmap);
        Paint paint = new Paint();
        Rect rect = new Rect(0,0,sBitmap.getWidth(),sBitmap.getHeight());
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setFilterBitmap(true);
        canvas.drawCircle(outBitmap.getWidth()/2-1f,outBitmap.getHeight()/2-1f,sBitmap.getWidth()/2 ,paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(sBitmap,rect,rect,paint);
        Paint paint1 = new Paint();
        paint1.setAntiAlias(true);
        paint1.setDither(true);
        paint1.setFilterBitmap(true);
        paint1.setStyle(Paint.Style.STROKE);
        paint1.setStrokeWidth(0.5f);
        paint1.setColor(Color.GRAY);
        canvas.drawCircle(sBitmap.getWidth()/2 + 2f,sBitmap.getHeight()/2 +2f,sBitmap.getWidth()/2+0.5f ,paint1);
        return outBitmap;
    }
}
